﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace UserManagementSystem
{
    public class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }

    public class UserSystem
    {
        private Dictionary<string, User> _users = new Dictionary<string, User>();

        public void Run()
        {
            bool isRunning = true;
            
            while (isRunning)
            { 
                Console.Clear();
                Console.WriteLine("===== 用户管理系统 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.WriteLine("========================");
                Console.Write("请选择功能 (1-3): ");
                
                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        Login();
                        break;
                    case "2":
                        Register();
                        break;
                    case "3":
                        isRunning = false;
                        Console.WriteLine("谢谢使用，再见！");
                        break;
                    default:
                        Console.WriteLine("请输入有效数字 (1-3)");
                        WaitForKey();
                        break;
                }
            }
        }
        private void Register()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");
            string username = GetValidUsername();
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("注册取消");
                WaitForKey();
                return;
            }
            
            string password = GetValidPassword();
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("注册取消");
                WaitForKey();
                return;
            }
            string confirmPassword;
            do
            {
                Console.Write("请输入确认密码: ");
                confirmPassword = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(confirmPassword))
                {
                    Console.WriteLine("确认密码不可为空！");
                    continue;
                }

                if (confirmPassword != password)
                {
                    Console.WriteLine("两次输入的密码不一致，请重新输入！");
                }

            } while (confirmPassword != password);
            _users.Add(username, new User { Username = username, Password = password });
            Console.WriteLine("注册成功！");
            WaitForKey();
        }
        
        private void Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");
            Console.Write("请输入用户名: ");
            string username = Console.ReadLine().Trim();

            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("用户名不可为空！");
                WaitForKey();
                return;
            }
            if (!_users.ContainsKey(username))
            {
                Console.WriteLine("用户名不存在！");
                WaitForKey();
                return;
            }
            Console.Write("请输入密码: ");
            string password = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("密码不可为空！");
                WaitForKey();
                return;
            }
            if (_users[username].Password == password)
            {
                Console.WriteLine($"登录成功！欢迎回来，{username}！");
                Console.WriteLine("进入系统首页...");
                WaitForKey();
            }
            else
            {
                Console.WriteLine("密码错误！");
                WaitForKey();
            }
        }
        private string GetValidUsername()
        {
            string username;
            do
            {
                Console.Write("请输入用户名: ");
                username = Console.ReadLine()?.Trim() ?? "";
                if (string.IsNullOrWhiteSpace(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }

                // 检查长度
                if (username.Length < 3 || username.Length > 15)
                {
                    Console.WriteLine("用户名长度必须为3-15个字符！");
                    continue;
                }

                // 检查字符合法性
                if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("用户名只能包含字母、数字和下划线！");
                    continue;
                }

                // 检查用户名是否已存在
                if (_users.ContainsKey(username))
                {
                    Console.WriteLine("该用户名已被注册，请选择其他用户名！");
                    continue;
                }

                // 验证通过
                break;

            } while (true);

            return username;
        }

        // 获取并验证密码
        private string GetValidPassword()
        {
            string password;
            do
            {
                Console.Write("请输入密码: ");
                password = Console.ReadLine() ?? "";

                // 检查是否为空
                if (string.IsNullOrWhiteSpace(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }

                // 检查长度
                if (password.Length < 6 || password.Length > 20)
                {
                    Console.WriteLine("密码长度必须为6-20个字符！");
                    continue;
                }

                // 检查是否包含大写字母
                if (!Regex.IsMatch(password, @"[A-Z]"))
                {
                    Console.WriteLine("密码必须包含至少1个大写字母！");
                    continue;
                }

                // 检查是否包含小写字母
                if (!Regex.IsMatch(password, @"[a-z]"))
                {
                    Console.WriteLine("密码必须包含至少1个小写字母！");
                    continue;
                }

                // 检查是否包含数字
                if (!Regex.IsMatch(password, @"[0-9]"))
                {
                    Console.WriteLine("密码必须包含至少1个数字！");
                    continue;
                }

                // 验证通过
                break;

            } while (true);

            return password;
        }

        // 等待用户按键继续
        private void WaitForKey()
        {
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }

        // 程序入口
        public static void Main(string[] args)
        {
            UserSystem system = new UserSystem();
            system.Run();
        }
    }
}
